package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.entity.po.TableNumber;
import com.project.entity.vo.TableNumberVo;
import com.project.exception.ServiceException;
import com.project.mapper.TableNumberMapper;
import com.project.entity.qo.TableNumberQo;
import com.project.result.ResponseResult;
import com.project.result.ResultCode;
import com.project.service.TableNumberService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chen
 * @date 2022/3/28
 */
@Service
public class TableNumberServiceImpl implements TableNumberService {
    @Resource
    TableNumberMapper tableNumberMapper;

    /**
     * po对象转vo对象方法封装
     * @param tableNumbers  传递po对象
     * @return 返回 vo对象
     */
    private List<TableNumberVo> transform(List<TableNumber> tableNumbers){
        List<TableNumberVo> tableNumberVoList = new ArrayList<>();
        for (TableNumber t:tableNumbers) {
            TableNumberVo tableNumberVo = new TableNumberVo();
            BeanUtils.copyProperties(t,tableNumberVo);
            tableNumberVoList.add(tableNumberVo);
        }
        return tableNumberVoList;
    }
    /**
     *  添加桌号
     * @param tableNumberQo 传入桌号信息
     */
    @Override
    public ResponseResult<ResultCode> addTableNumber(TableNumberQo tableNumberQo) {
        TableNumber tableNumber = new TableNumber();
        BeanUtils.copyProperties(tableNumberQo,tableNumber);
        int insert = tableNumberMapper.insert(tableNumber);
        if (insert == 0){
            //添加失败,抛出异常
            throw new ServiceException(ResultCode.SYSTEM_UNKNOW_ERROR);
        }
        //添加成功
        return ResponseResult.success(ResultCode.SUCCESS);
    }

    /**
     *  根据桌号修改信息
     * @param tableNumberQo 传入桌号信息
     * @return 返回状态值
     */
    @Override
    public ResponseResult<ResultCode> updateTableNumber(TableNumberQo tableNumberQo) {
        TableNumber tableNumber = new TableNumber();
        BeanUtils.copyProperties(tableNumberQo,tableNumber);
        int update = tableNumberMapper.updateById(tableNumber);
        if(update == 0){
            //修改失败,抛出异常
            throw new ServiceException(ResultCode.SYSTEM_UNKNOW_ERROR);
        }
        //修改成功
        return ResponseResult.success(ResultCode.SUCCESS);
    }

    /**
     * 根据桌号修改该桌位使用状态
     * @param tableNum 桌号
     * @return 返回状态值
     */
    @Override
    public ResponseResult<ResultCode> updateTableStatus(Integer tableNum,Integer status) {
        TableNumber tableNumber = new TableNumber();
        tableNumber.setTableNumber(tableNum);
        tableNumber.setStatus(status);
        int update = tableNumberMapper.updateById(tableNumber);
        if(update == 0){
            //修改失败,抛出异常
            throw new ServiceException(ResultCode.SYSTEM_UNKNOW_ERROR);
        }
        //修改成功
        return ResponseResult.success(ResultCode.SUCCESS);
    }

    /**
     * 查询所有桌位信息
     * @return 返回所有桌位信息
     */
    @Override
    public ResponseResult<List<TableNumberVo>> getTableNumber() {
        List<TableNumber> tableNumbers = tableNumberMapper.selectList(null);
        if(tableNumbers == null){
            //查询失败
            throw new ServiceException(ResultCode.SYSTEM_INNER_ERROR);
        }
        List<TableNumberVo> tableNumberVoList = transform(tableNumbers);
        return ResponseResult.success(ResultCode.SUCCESS,tableNumberVoList);
    }

    /**
     * 根据桌位获取该桌位信息
     * @param tableNum 桌号
     * @return 返回该桌号桌位信息
     */
    @Override
    public ResponseResult<TableNumberVo> getTableNumberById(Integer tableNum) {
        QueryWrapper<TableNumber> qw = new QueryWrapper<>();
        qw.lambda().eq(TableNumber::getTableNumber,tableNum);
        TableNumber tableNumber = tableNumberMapper.selectOne(qw);
        if (ObjectUtils.isEmpty(tableNumber)){
            throw new ServiceException(ResultCode.SYSTEM_UNKNOW_ERROR);
        }
        TableNumberVo tableNumberVo = new TableNumberVo();
        BeanUtils.copyProperties(tableNumber,tableNumberVo);
        return ResponseResult.success(ResultCode.SUCCESS,tableNumberVo);
    }

    /**
     * 获取空座位桌号
     * @return 返回所有空桌位信息
     */
    @Override
    public ResponseResult<List<TableNumberVo>> getTableNumberByStatus() {
        List<TableNumber> tableNumbers = tableNumberMapper.getTableNumberByStatus();
        if(ObjectUtils.isEmpty(tableNumbers)){
            throw  new ServiceException(ResultCode.SYSTEM_INNER_ERROR);
        }
        List<TableNumberVo> tableNumberVoList = transform(tableNumbers);
        return ResponseResult.success(ResultCode.SUCCESS,tableNumberVoList);
    }

    /**
     * 分页查询
     * @param current 当前页数
     * @return 当前页的数据
     */
    @Override
    public ResponseResult<List<TableNumberVo>> selectTableNumberPage(Integer current) {
        //传递当前页和每页数量
        Page<TableNumber> page = new Page<>(current,5);
        QueryWrapper<TableNumber>  qw = new QueryWrapper<>();
        qw.lambda().eq(TableNumber::getIsDelete,1);
        Page<TableNumber> tableNumberPage = tableNumberMapper.selectPage(page, qw);
        if(ObjectUtils.isEmpty(tableNumberPage)){
            throw new ServiceException(ResultCode.SYSTEM_INNER_ERROR);
        }
        //获取当前页数据
        List<TableNumber> tableNumbers = tableNumberPage.getRecords();

        List<TableNumberVo> tableNumberVoList = transform(tableNumbers);
        return ResponseResult.success(ResultCode.SUCCESS,tableNumberVoList);
    }


}
